package com.space.school.controller;

import com.space.school.domain.Category;
import com.space.school.domain.User;
import com.space.school.services.CategoryService;
import com.space.school.services.impl.CategoryServiceImpl;
import com.space.school.utils.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "categoryServlet",value = "/categoryServlet")
public class CategoryServlet extends HttpServlet {
    CategoryService categoryService=new CategoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        查询全部信息
        //   设置编码格式
        req.setCharacterEncoding("UTF-8");
        //        获取当前页数
        String currentPage=req.getParameter("currentPage");
//        如果第一次进页面,当前页数为空或为null
        if(currentPage==null || currentPage.equals("")){
            currentPage="1";
        }
        //       每页显示页数
        int pageSize=3;
        //        取查询条件
        Map<String,String[]> map=req.getParameterMap();
        PageBean<Category> pb=categoryService.search(Integer.parseInt(currentPage),pageSize,map);
        req.setAttribute("pb",pb);
        req.setAttribute("category_name",req.getParameter(req.getParameter("category_name")));
        req.setAttribute("category_type",req.getParameter(req.getParameter("category_type")));
//        跳转页面
        req.getRequestDispatcher("categoryList.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //        获取当前页数
        String currentPage=req.getParameter("currentPage");
//        如果第一次进页面,当前页数为空或为null
        if(currentPage==null || currentPage.equals("")){
            currentPage="1";
        }
        //        进行数据查询 findAll 的方法
        //        将查询的结果集个放进页面
//       每页显示页数
        int pageSize=3;
        Map<String,String[]> map=req.getParameterMap();
        PageBean<Category> pb=categoryService.search(Integer.parseInt(currentPage),pageSize,map);
        req.setAttribute("pb",pb);
        String name=req.getParameter("category_name");
        req.setAttribute("category_name",name);
        String type=req.getParameter("category_type");
        req.setAttribute("category_type",type);
//        跳转页面
        req.getRequestDispatcher("categoryList.jsp").forward(req,resp);
    }
}
